﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace PHTShop.admin
{
    public partial class createAdmin : System.Web.UI.Page
    {
        DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "create new account";

        }

        protected void btnOK_Click(object sender,EventArgs e )
        {
            if (Session["admin"] != null)
            {
                Account acc = new Account();        
                     acc.username = txtUsername.Value;
                    if (dc.Accounts.Where(acs => acs.username.Equals(acc.username)).ToList().Count() > 0)
                    {
                    lblmesage.InnerHtml ="Username is exists";
                    }
                    else
                    {        
                        acc.password = FormsAuthentication.HashPasswordForStoringInConfigFile(txtNewPassword.Value, "SHA1");
                        acc.phone = txtPhone.Value;
                        acc.roleId = 1;
                        acc.fullname = txtFullName.Value;
                        foreach (ListItem item in RadioButtonList1.Items)
                        { 
                            if(item.Selected)
                                acc.roleId = Convert.ToInt16(item.Value);
                        }
                        acc.active = CheckBox1.Checked;
                        acc.address = txtAddress.Value;
                        acc.email = txtEmail.Value;
                        try
                        {
                            dc.Accounts.InsertOnSubmit(acc);
                            dc.SubmitChanges();
                            Response.Redirect("~/admin/manangeUser.aspx");
                        }
                        catch(Exception){
                        lblmesage.InnerHtml = "Create failed";}
                    }

                    clear();
                    
               
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        
        }
        private void clear()
        {
            txtUsername.Value = "";
            txtAddress.Value = "";

            txtNewPassword.Value = "";
            txtConfirmPassword.Value = "";
            txtEmail.Value = "";
            txtPhone.Value = "";
            txtFullName.Value = "";
                      
        }

    }
}